package com.stylefeng.guns.modular.pay.controller.api;

import java.util.List;
import java.util.Locale;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.stylefeng.guns.GunsApplication;
import com.stylefeng.guns.core.util.AjaxRes;
import com.stylefeng.guns.modular.pay.dto.ApiMchRegParams;
import com.stylefeng.guns.modular.pay.exception.ApiException;
import com.stylefeng.guns.modular.pay.model.BankCode;
import com.stylefeng.guns.modular.pay.model.SubBankCode;
import com.stylefeng.guns.modular.pay.service.IBankCodeService;
import com.stylefeng.guns.modular.pay.service.IJftService;
import com.stylefeng.guns.modular.pay.service.IMchRegService;
import com.stylefeng.guns.modular.pay.service.ISubBankCodeService;
import com.stylefeng.guns.modular.pay.service.impl.IdWorkerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 会员注册业务接口
 * @author Administrator
 *
 */
@Api(value="商户注册、认证业务接口",tags="商户注册、认证业务接口")
@Controller
@RequestMapping("/api/mchReg/")
public class ApiMchRegController {
    private final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);
    ObjectMapper mapper = new ObjectMapper();
	private String PREFIX = "/api/";
	private static Validator validator;
	@Autowired
	private IdWorkerService idWorkerService;
	@Autowired
	private IBankCodeService bankCodeService;
	@Autowired
	private ISubBankCodeService subBankCodeService;
	@Autowired
	private IMchRegService mchRegService;
	@Autowired
	private IJftService jftService;
	
	
	static {
		Locale.setDefault(Locale.ENGLISH); // speicify language
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}
	
	@ApiOperation(value = "银行列表", notes = "银行列表")
	@RequestMapping(value = "/banks", method = RequestMethod.GET)
	@ResponseBody
	 public AjaxRes banks() {
		 //获取银行名称
		 List<BankCode> bankList = bankCodeService.selectList(new EntityWrapper<BankCode>().orderBy(" id asc"));
		 return new AjaxRes(AjaxRes.SUCCESS, "成功",bankList);
		 
	 }
	
	
	@ApiOperation(value = "联行号", notes = "联行号")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "bankName", value = "银行名称（接口获取）", required = true, dataType = "String"),
		@ApiImplicitParam(name = "keywords", value = "分行民称关键字", required = true, dataType = "String"),
	})
	@RequestMapping(value = "/subBankList", method = RequestMethod.GET)
	@ResponseBody
	 public AjaxRes subBankList(@RequestParam("bankName")String bankName,@RequestParam("keywords")String keywords) {
		 //获取银行名称
		if(Strings.isNullOrEmpty(bankName)){
			return new AjaxRes("empty.bankName", "银行名称不能为空");
		}
		if(Strings.isNullOrEmpty(keywords)){
			return new AjaxRes("empty.keywords", "关键字不能为空");
		}
		List<SubBankCode> subBankList = subBankCodeService.selectList(new EntityWrapper<SubBankCode>().eq("bank_name", bankName)
				 .like("branch_bank_name",keywords));
//		if(subBankList!=null && subBankList.size()==0){
//			
//		}
		return new AjaxRes(AjaxRes.SUCCESS, "成功",subBankList);
	 }
	
	@ApiOperation(value = "银行名称", notes = "银行名称")
	@RequestMapping(value = "/bankNameList", method = RequestMethod.GET)
	@ResponseBody
	 public AjaxRes bankNameList() {
		 //获取银行名称
		 List<String> bankNameList = subBankCodeService.bankNameList();
		 return new AjaxRes(AjaxRes.SUCCESS, "成功",bankNameList);
	 }
	 

	@ApiOperation(value = "商户认证", notes = "商户认证")

	@RequestMapping(value = "/auth", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes auth(ApiMchRegParams params) {
		AjaxRes ajax = null;
		try {
			logger.info("请求参数："+params);
			ajax= jftService.memberReg(params);
		} catch (ApiException e) {
				return new AjaxRes(e.getRetCode(), e.getErrMsg());
		}catch (Exception e) {
			e.printStackTrace();
			return new AjaxRes("unexception", "未知错误"); 
		}
		return ajax;
	}
	
	@ApiOperation(value = "业务开通", notes = "业务开通")
	@ApiImplicitParam(name = "chMerCode", value = "商户号", required = true, dataType = "String")
	@RequestMapping(value = "/regBus", method = RequestMethod.POST)
	@ResponseBody
	 public AjaxRes regBus(String chMerCode) {
		 //获取银行名称s
		return jftService.regBus(chMerCode);
	 }
	
//	@ApiOperation(value = "联行号导入", notes = "联行号导入")
//	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//	@ResponseBody
//	public String importExcel() {
//		ReturnConfig returnConfig = ExcelConfigFactory.createExcelConfigManger().getModelConfig("subBankCodeList");
//		 ExcelToModel excelToModel = null;
//		try {
//			 String path = "c://分支行联行号.xls";
//			 InputStream inputStream = new FileInputStream(path);
//			 excelToModel = new ExcelToModelImpl(inputStream, returnConfig);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		List<SubBankCode> list = null;
//		try {
//			list = excelToModel.getModeList(0);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		if(list!=null && list.size()>0){
//			subBankCodeService.insertBatch(list);
//		}
//		return "success";
//	}
	
	

}
